const app = getApp();
const ipUrl = app.globalData.ipUrl;
import{
  getVediosType,getVedios,getRandomVideos
}from "../../apis/getVideos"
Page({
  clickCell: function(event) {
    console.log(event)
    const newsurl = event.currentTarget.id;
    const nameid1 = this.data.nameid
    const nameid2 = event.currentTarget.dataset.value
    const Array = event.currentTarget.dataset.value2
    console.log(Array)
    wx.redirectTo({
      url: `/pages/webViewVideo/webViewVideo?url=${encodeURIComponent(newsurl)}&nameid=${encodeURIComponent(nameid1)}&nameid2=${encodeURIComponent(nameid2)}&Array=${encodeURIComponent(JSON.stringify(Array))}`
    });
  },
  onClick(event) {

    this.data.videoSet.followed = !this.data.videoSet.followed;
    this.setData({
      videoSet: this.data.videoSet
    })

  },
  changeAgreed(event) {

    this.data.videoSet.agreed = !this.data.videoSet.agreed;
    if( /^\d+$/.test(this.data.videoSet.agreeNumber)){
    this.data.videoSet.agreeNumber = this.data.videoSet.agreeNumber + 1;
    }
    this.setData({
      videoSet: this.data.videoSet
    })
  },
  changeAgreed2(event) {

    this.data.videoSet.agreed = !this.data.videoSet.agreed;
    if(/^\d+$/.test(this.data.videoSet.agreeNumber)){
      this.data.videoSet.agreeNumber = this.data.videoSet.agreeNumber - 1;
    }

    this.setData({
      videoSet: this.data.videoSet
    })
  },
  changeCollected(event) {

    this.data.videoSet.collected = !this.data.videoSet.collected;
    this.setData({
      videoSet: this.data.videoSet
    })
  },
  data: {
    nameid2:"",
    nameid:"",
    ipUrl:"",
    videoUrl: '',
    videoSet:{
      
    },
    video:[
    ],
  },
  onLoad: function(options) {

    this.setData({
      ipUrl:ipUrl
    })
    console.log(options); // 确认 options 对象的结构

    if (options.nameid2) {
      const decodedUrl = decodeURIComponent(options.nameid2);
      this.setData({
        nameid2: decodedUrl
      });
      console.log(this.data.nameid2); // 确认 webUrl 的值

      
    }
    if (options.url) {
      const decodedUrl = decodeURIComponent(options.url);
      this.setData({
        videoUrl: decodedUrl
      });
      console.log(this.data.videoUrl); // 确认 webUrl 的值
    }   else{
      console.error('URL 参数缺失');
    }
     if(options.nameid) {
      const decodedUrl = decodeURIComponent(options.nameid);
      this.setData({
        nameid: decodedUrl
      });
      console.log(this.data.nameid); // 确认 webUrl 的值

    }
    else{
      console.error('nameid 参数缺失');
    }
    if(this.data.nameid!=="random"){
      getVedios(this.data.nameid).then((res)=>{
        console.log(res);
        this.setData({
          video: res.vediolist
            
        })
        let i = 0;
        for(i=0;i<this.data.video.length;i++)
        {
          if(this.data.video[i].watched>=10000)
          {
            this.data.video[i].watched = this.data.video[i].watched/10000+"万";
          }
          if(this.data.video[i].agreeNumber>=10000)
          {
            this.data.video[i].agreeNumber = this.data.video[i].agreeNumber/10000+"万";
          }
          let DataArray = this.data.video
          this.setData({
            video:DataArray
          })
        console.log(this.data.video)
        }
        console.log(this.data.video);
        this.setData({
          videoSet:this.data.video[this.data.nameid2]
        });
  
    })
    }else{
      const decodedUrl = JSON.parse(decodeURIComponent(options.Array));
      this.setData({
        video: decodedUrl
      });
      console.log(this.data.video);
        let i = 0;
        for(i=0;i<this.data.video.length;i++)
        {
          if(this.data.video[i].watched>=10000)
          {
            this.data.video[i].watched = this.data.video[i].watched/10000+"万";
          }
          if(this.data.video[i].agreeNumber>=10000)
          {
            this.data.video[i].agreeNumber = this.data.video[i].agreeNumber/10000+"万";
          }
          let DataArray = this.data.video
          this.setData({
            video:DataArray
          })

        }
        console.log(this.data.video);
        this.setData({
          videoSet:this.data.video[this.data.nameid2]
        });
  
    
    }


  }
});
